home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Interactive Web Graphics with Shout 3D
/
Interactive Web Graphics With Shout 3D.iso
/
mac
/
Code
/
Chapter10
/
TargetViewpoint.java
< prev
next >
Wrap
Text File
|
2000-07-17
|
3KB
|
97 lines
package custom_nodes;
import shout3d.core.*;
import shout3d.math.*;
/**
* TargetViewpoint
*
*/
public class TargetViewpoint extends Viewpoint {
float[] origin = {0.0f, 0.0f, 0.0f};
final public FloatField heading = new FloatField(this, "heading", Field.ANY, 0);
final public FloatField pitch = new FloatField(this, "pitch", Field.ANY, 0);
final public FloatField distance = new FloatField(this, "distance", Field.NON_NEGATIVE_FLOAT, 10);
final public FloatArrayField center = new FloatArrayField(this, "center", Field.COORD3, origin);
Quaternion q = new Quaternion();
float[] eulers = {0.0f, 0.0f, 0.0f};
public TargetViewpoint(){
this(null);
}
public TargetViewpoint(Shout3DViewer viewer){
super(viewer);
heading.addFieldObserver(this, null);
pitch.addFieldObserver(this, null);
distance.addFieldObserver(this, null);
center.addFieldObserver(this,null);
setViewpoint();
}
protected void finalize() throws Throwable {
heading.removeFieldObserver(this);
pitch.removeFieldObserver(this);
distance.removeFieldObserver(this);
center.removeFieldObserver(this);
super.finalize();
}
public void onFieldChange(Field theField, Object userData) {
if ( theField == heading || theField == pitch || theField == distance || theField == center) {
setViewpoint();
}
else {
// call super class--Viewpoint
super.onFieldChange(theField, userData);
}
}
public void setViewpoint() {
//set quaternion to current heading and pitch
eulers[0] = heading.getValue();
eulers[1] = pitch.getValue();
q.setEulers(eulers);
//set distance vector
//and rotate it
if ( distance.getValue() < 0.0f) {
distance.setValue(0.0f);
}
float[] vector = {0.0f, 0.0f, distance.getValue()};
q.xform(vector);
//translate vector relative
//to target center
vector[0] = vector[0] + center.getValue()[0];
vector[1] = vector[1] + center.getValue()[1];
vector[2] = vector[2] + center.getValue()[2];
//set Viewpoint rotation and
//translate to end of vector
float[] axisAngle = new float[4];
q.getAxisAngle(axisAngle);
orientation.setValue(axisAngle);
position.setValue(vector);
}
}